
$(function () {
    var up = $("#up");
    var down = $("#down");
    var first = $("#first");
    var second = $("#second");
    var third = $("#third");
    var last = $("#last");
    var currentNo = $("#pageNo");
    var pageNo = 1;

    var p1 = first.attr("name");
    var p2 = second.attr("name");
    var p3 = third.attr("name");

    up.bind("click",function () {
        pageNo = Number(currentNo.val())-1;
        if(pageNo>1){
            currentNo.val(pageNo);
            sub(pageNo)
        }else {
            currentNo.val(1);
            sub(1);
        }
    });

    down.bind("click",function () {
        pageNo = Number(currentNo.val())+1;
        if(pageNo<last.attr("name")){
            currentNo.val(pageNo);
            sub(pageNo);
        } else{
            currentNo.val(last.attr("name"));
            sub(last.attr("name"));
        }
    });

    last.bind("click",function () {
        var vlast = last.attr("name");
        sub(vlast);
    });

    first.bind("click",function () {
        currentNo.val(1);
        sub(p1);
    });

    second.bind("click",function () {
        currentNo.val(p2);
        sub(p2);
    });

    third.bind("click",function () {
        currentNo.val(p3);
        sub(p3);
    });


    var tab = $(".mem_tab");
    $.ajax({
        url : "easybuy/product/orderCount",
        type : "get",
        success : function (data) {
            if(data !=null){
                //alert(data);
                //num = data;
                //for(var i=1;i<=data;i++){
                    $.ajax({
                        url : "easybuy/product/oderList",
                        type : "get",
                        dataType : "json",
                        async : false,
                        data : {"aid": 1,"bid": 1},
                        success : function (data) {

                            orderCount();
                            var th = "";
                            th += "<tr style='background-color: #ffb474'><td style='height: 30px;width: 300px'>用户名："+data[0].order.loginName+"</td>";
                            th += "<td style='height: 30px;width: 500px'>订单号："+data[0].order.serialNumber+"</td>";
                            th += "<td style='height: 30px;width: 500px'>地址："+data[0].order.userAddress+"</td>";
                            th += "<td style='height: 30px;'> "+accounting.formatMoney(data[0].order.cost,"￥", 1, ",", ".")+"</td></tr>";
                            tab.append(th);
                            var tj = "";
                            tj += "<tr><td style='height: 30px;width: 300px'>商品名称</td>";
                            tj += "<td style='height: 30px;width: 400px'>商品图片</td>";
                            tj += "<td style='height: 30px;width: 100px'>数量</td>";
                            tj += "<td style='height: 30px;width: 600px'>价格</td></tr>";
                            tab.append(tj);
                            var tk = "";
                            for(var j=0;j<data.length;j++){
                                tk += "<tr><td style='height: 40px;width: 300px'>"+data[j].name+"</td>";
                                tk += "<td style='height: 40px;width: 400px'><img src='../images/"+data[j].fileName+"' alt='' style='width: 100px;height: 100px;border: 1px solid gray'></td>";
                                tk += "<td style='height: 40px;width: 100px'>"+data[j].order_detail.quantity+"</td>"
                                tk += "<td style='height: 40px;width: 600px'>"+accounting.formatMoney(data[j].price,"￥", 1, ",", ".")+"</td>";
                            }
                            tab.append(tk);
                        }
                    })
                //}
            }

        },
        error : function () {
            alert("出现错误");
        }
    });

    function sub(pageNo){
        tab.html("");
        $.ajax({
            url : "easybuy/product/oderList",
            type : "get",
            dataType : "json",
            async : false,
            data : {"aid": pageNo,"bid": pageNo},
            success : function (data) {
                var th = "";
                th += "<tr style='background-color: #ffb474'><td style='height: 40px;width: 300px'>用户名："+data[0].order.loginName+"</td>";
                th += "<td style='height: 40px;width: 500px'>订单号："+data[0].order.serialNumber+"</td>";
                th += "<td style='height: 40px;width: 500px'>地址："+data[0].order.userAddress+"</td>";
                th += "<td style='height: 40px;'> "+accounting.formatMoney(data[0].order.cost,"￥", 1, ",", ".")+"</td></tr>";
                tab.append(th);
                var tj = "";
                tj += "<tr><td style='height: 40px;width: 300px'>商品名称</td>";
                tj += "<td style='height: 40px;width: 400px'>商品图片</td>";
                tj += "<td style='height: 40px;width: 100px'>数量</td>";
                tj += "<td style='height: 40px;width: 600px'>价格</td></tr>";
                tab.append(tj);
                var tk = "";
                for(var j=0;j<data.length;j++){
                    tk += "<tr><td style='height: 40px;width: 300px'>"+data[j].name+"</td>";
                    tk += "<td style='height: 40px;width: 400px'><img src='../images/"+data[j].fileName+"' alt='' style='width: 100px;height: 100px;border: 1px solid gray'></td>";
                    tk += "<td style='height: 40px;width: 100px'>"+data[j].order_detail.quantity+"</td>"
                    tk += "<td style='height: 40px;width: 600px'>"+accounting.formatMoney(data[j].price,"￥", 1, ",", ".")+"</td>";
                }
                tab.append(tk);
            },
            error : function(){
                alert("出现错误");
            }
        });
    }

    function orderCount(){
        $.ajax({
            url : "easybuy/product/orderCount",
            dataType : "json",
            type : "post",
            success : function (data) {
                if(data !=null){
                    last.attr("name",data);
                }
            },
            error : function(){
                alert("出现错误");
            }
        });
    }
});


